package com.rp.user.controller;


import com.alibaba.fastjson.JSON;
import com.rp.news.common.PagedResult;
import com.rp.news.common.RedisCommon;
import com.rp.news.common.ResponseStatusEnum;
import com.rp.news.common.Result;
import com.rp.news.enums.UserStatus;
import com.rp.news.util.RedisOperator;
import com.rp.pojo.AppUser;
import com.rp.user.service.AppUserMngService;
import com.rp.user.service.UserService;
import com.rp.vo.QueryUserListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.validation.Valid;

@Api(value = "用户管理相关的接口", tags = {"用户管理相关的接口 api文档"})
@RequestMapping("/appUser")
@RestController
@Slf4j
public class AppUserMngController {

    @Autowired
    private AppUserMngService appUserMngService;

    @Autowired
    private RedisOperator redis;

    @Autowired
    private UserService userService;

    @Autowired
    public RestTemplate restTemplate;


    @ApiOperation(value = "查询用户管理所有用户", notes = "查询用户管理所有用户")
    @PostMapping("/queryAll")
    public Result queryAll(@RequestBody @Valid QueryUserListVo vo) {
        log.info("查询用户管理所有用户vo = {}", JSON.toJSONString(vo));
        PagedResult result = appUserMngService.queryAll(vo);
        log.info("查询用户管理所有用户result = {}", JSON.toJSONString(result));
        return Result.ok(result);
    }

    @ApiOperation(value = "查询用户详情", notes = "查询用户详情", httpMethod = "POST")
    @PostMapping("/userDetail")
    public Result userDetail(String userId) {
        AppUser user = userService.getUser(userId);
        return Result.ok(user);
    }

    @ApiOperation(value = "冻结或者解冻用户", notes = "冻结或者解冻用户", httpMethod = "POST")
    @PostMapping("/freezeUserOrNot")
    public Result freezeUserOrNot(String userId, Integer doStatus) {
        if (!UserStatus.isUserStatusValid(doStatus)) {
            return Result.errorCustom(ResponseStatusEnum.USER_STATUS_ERROR);
        }
        appUserMngService.freezeUserOrNot(userId, doStatus);
        //todo 处理用户信息
        redis.del(RedisCommon.REDIS_USER_INFO + ":" + userId);
        return Result.ok();
    }
}
